package com.zx.platform.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zx.map.service.NoticeLogService;
import com.zx.map.service.domain.NoticeLogDomain;
import com.zx.map.service.model.NoticeLogModel;
import com.zx.platform.utils.PmpUtils;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 公告通知
 * @author ysm
 * @version 1.0
 */

@Controller
@RequestMapping("/manage/notice")
public class NoticeController {
	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(NoticeController.class);

	@Autowired
	private NoticeLogService noticeLogService;

	/**
	 * 进入列表页面
	 * @param model
	 * @param request
	 */
	@RequestMapping("/list")
	public String list(NoticeLogDomain domain, Model model,
			HttpServletRequest request) {
		try {// 初始化信息
			Integer pageNum = domain.getPageNum();
			Integer pageSize = domain.getPageSize();
			Integer pageCount = domain.getPageCount();
			if (null == pageNum) {
				pageNum = 1;
			}
			if (null == pageSize) {
				pageSize = 10;
			}
			Map map = noticeLogService.getPageByModel(pageSize, pageNum, domain);
			List<NoticeLogModel> noticeList = (List<NoticeLogModel>) map.get("list");
			int recordCount = Integer.valueOf(map.get("count").toString());
			// 页面信息
			if (pageSize * pageNum > recordCount + pageSize - 1) {
				pageNum = 1;
			}
			model.addAttribute("list", noticeList);
			model.addAttribute("pageNum", pageNum);
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageCount",
					PmpUtils.getPageCount(recordCount, pageSize));
			model.addAttribute("recordCount", recordCount);
			model.addAttribute("model", domain);
			return "/jsp/platform/notice/noticeList";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	/**
	 * 进入新增公告页面
	 */
	@RequestMapping("/toAddNotice")
	public String toAddNotice(Model model, HttpServletRequest request) {
		try {// 初始化信息
			return "/jsp/platform/notice/addNotice";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	/**
	 * 新增公告
	 */
	@RequestMapping("/addNotice")
	public String addNotice(NoticeLogModel model, HttpServletRequest request) {
		try {// 初始化信息

			model.setCreateDate(new Date());
			model.setNoticeType("1");
			noticeLogService.add(model);
			model.setSuccess(true);
			model.setMsg("新增公告信息成功");
			log.debug("新增公告信息成功");
		} catch (Exception e) {
			model.setSuccess(false);
			model.setMsg("新增公告信息失败");
			log.error("新增公告信息失败" + e.getMessage(), e);
		}
		return "redirect:/manage/notice/list";
	}
}
